﻿using System.Collections.Generic;
using System.Text;
using Ext.Net;

namespace KamLib
{

    public enum UilNotificationKind { Info, Error, Warning }

    public class UilNotification
    {
        public UilNotificationKind Kind;
        public string Title;
        public string Message;
        public bool? Pinned;
        public int? Width;
        public int? Height;

        public class List : List<UilNotification>
        {
            public UilNotification Add(string Message,
                string Title = null, UilNotificationKind Kind = UilNotificationKind.Info,
                bool? Pinned = null, int? Width = null, int? Height = null
            )
            {
                var ntf = new UilNotification
                {
                    Kind = Kind,
                    Title = Title,
                    Message = Message,
                };

                if (Pinned != null) ntf.Pinned = Pinned.Value;
                if (Width != null) ntf.Width = Width.Value;
                if (Height != null) ntf.Height = Height.Value;


                Add(ntf);
                return ntf;
            }

            public UilNotification AddInfo(string Message, string Title = null, bool? Pinned = null, int? Width = null, int? Height = null)
            {
                return Add(Message, Title, UilNotificationKind.Info, Pinned, Width, Height);
            }

            public UilNotification AddError(string Message, string Title = null, bool? Pinned = null, int? Width = null, int? Height = null)
            {
                return Add(Message, Title, UilNotificationKind.Error, Pinned, Width, Height);
            }

            public UilNotification AddWarning(string Message, string Title = null, bool? Pinned = null, int? Width = null, int? Height = null)
            {
                return Add(Message, Title, UilNotificationKind.Warning, Pinned, Width, Height);
            }

            public void Show()
            {
                foreach (var item in this)
                {
                    item.Show();
                }
            }

            public string ToScript()
            {
                var sb = new StringBuilder();
                foreach (var item in this)
                {
                    sb.AppendLine(item.ToScript());
                }
                return sb.ToString();
            }

        }


        public void Show()
        {
            Notification.Show(NewNotificationConfig());
        }
        public string ToScript()
        {
            return NewNotificationConfig().ToScript();
        }

        protected NotificationConfig NewNotificationConfig()
        {
            var n = new NotificationConfig
            {
                Title = Title,
                Html = Message,
                AutoScroll = true,
            };

            if (Pinned != null)
            {
                n.Pinned = Pinned.Value;
                if (Pinned == true)
                    n.ShowPin = true;
            }
            if (Width != null) n.Width = Width.Value;
            if (Height != null) n.Height = Height.Value;

            switch (Kind)
            {
                case UilNotificationKind.Error: n.Icon = Icon.Error; break;
                case UilNotificationKind.Info: n.Icon = Icon.Information; break;
            }

            return n;
        }

    }

}
